package com.nanhai.buddhist.college.module.study.controller.admin.studysubject;

import com.nanhai.buddhist.college.framework.common.util.date.DateUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;

import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;

import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.nanhai.buddhist.college.module.study.controller.admin.studysubject.vo.*;
import com.nanhai.buddhist.college.module.study.dal.dataobject.studysubject.StudySubjectDO;
import com.nanhai.buddhist.college.module.study.convert.studysubject.StudySubjectConvert;
import com.nanhai.buddhist.college.module.study.service.studysubject.StudySubjectService;

@Api(tags = "管理后台 - 课程管理")
@RestController
@RequestMapping("/study/subject")
@Validated
public class StudySubjectController {

    @Resource
    private StudySubjectService subjectService;

    @PostMapping("/create")
    @ApiOperation("创建课程管理")
    //@PreAuthorize("@ss.hasPermission('study:subject:create')")
    public CommonResult<String> createSubject(@Valid @RequestBody StudySubjectCreateReqVO createReqVO) {
        return success(subjectService.createSubject(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新课程管理")
    //@PreAuthorize("@ss.hasPermission('study:subject:update')")
    public CommonResult<Boolean> updateSubject(@Valid @RequestBody StudySubjectUpdateReqVO updateReqVO) {
        subjectService.updateSubject(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除课程管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
   // @PreAuthorize("@ss.hasPermission('study:subject:delete')")
    public CommonResult<Boolean> deleteSubject(@RequestParam("id") String id) {
        subjectService.deleteSubject(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得课程管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
   // @PreAuthorize("@ss.hasPermission('study:subject:query')")
    public CommonResult<StudySubjectRespVO> getSubject(@RequestParam("id") String id) {
        StudySubjectDO subject = subjectService.getSubject(id);
        return success(StudySubjectConvert.INSTANCE.convert(subject));
    }

    @GetMapping("/list")
    @ApiOperation("获得课程管理列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
   // @PreAuthorize("@ss.hasPermission('study:subject:query')")
    public CommonResult<List<StudySubjectRespVO>> getSubjectList(@RequestParam("ids") Collection<String> ids) {
        List<StudySubjectDO> list = subjectService.getSubjectList(ids);
        return success(StudySubjectConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得课程管理分页")
    //@PreAuthorize("@ss.hasPermission('study:subject:query')")
    public CommonResult<PageResult<StudySubjectRespVO>> getSubjectPage(@Valid StudySubjectPageReqVO pageVO) {
        PageResult<StudySubjectDO> pageResult = subjectService.getSubjectPage(pageVO);

        if(pageVO.getCreateTime()!=null && pageVO.getCreateTime().length>1){
            Date endDateTime = pageVO.getCreateTime()[1];
            pageVO.getCreateTime()[1] = DateUtils.addDate(endDateTime,Calendar.DATE,1);
        }
        return success(StudySubjectConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出课程管理 Excel")
    //@PreAuthorize("@ss.hasPermission('study:subject:export')")
    @OperateLog(type = EXPORT)
    public void exportSubjectExcel(@Valid StudySubjectExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        if(exportReqVO.getCreateTime()!=null && exportReqVO.getCreateTime().length>1){
            Date endDateTime = exportReqVO.getCreateTime()[1];
            exportReqVO.getCreateTime()[1] = DateUtils.addDate(endDateTime,Calendar.DATE,1);
        }
        List<StudySubjectDO> list = subjectService.getSubjectList(exportReqVO);
        // 导出 Excel
        List<StudySubjectExcelVO> datas = StudySubjectConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "课程管理.xls", "数据", StudySubjectExcelVO.class, datas);
    }

    @GetMapping("/byFindStr/list")
    @ApiOperation("获得课程列表--可以根据昵称模糊查询")
    @ApiImplicitParam(name = "findStr", value = "查询条件", required = true, example = "1024,2048", dataTypeClass = String.class)
    public CommonResult<List<StudySubjectRespVO>> getStudySubjectDOListByStr(String findStr) {
        List<StudySubjectDO> list = subjectService.getStudySubjectDOListByStr(findStr);
        return success(StudySubjectConvert.INSTANCE.convertList(list));
    }

}
